/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.common.customize.i18n.config;

import com.pig4cloud.pigx.common.customize.i18n.interceptors.CustomerLocaleChangeInterceptors;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

/**
 * Dapp国际化配置
 *
 * @Date: 2022/8/29 16:58
 */
@Configuration
public class MessageSourceConfiguration {


    /**
     * 配置i18n目录
     */
    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames(
                "classpath:i18n/CommonApiMessages",
                //dapp系统模块
                "classpath:i18n/systemMessages",
                //用户模块
                "classpath:i18n/userMessages"
        );
        return messageSource;
    }


    /**
     * 默认解析器 其中locale表示默认语言
     */
    @Bean
    public LocaleResolver localeResolver() {
        Locale.setDefault(Locale.CHINA);
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(Locale.CHINA);
        return localeResolver;
    }


    /**
     * 默认拦截器 其中lang表示切换语言的参数名
     */
    @Bean
    public WebMvcConfigurer localeInterceptor() {
        return new WebMvcConfigurer() {
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                // LocaleChangeInterceptors只支持从URL参数读取语言参数，为了也能从headers头部读取语言参数，我们继承了LocaleChangeInterceptors，并重写了preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)方法
                CustomerLocaleChangeInterceptors localeInterceptor = new CustomerLocaleChangeInterceptors();
                localeInterceptor.setParamName("Dapp-Accept-Language");
                registry.addInterceptor(localeInterceptor);
            }
        };
    }


}
